//
//  CAnAlgExerVitalSignsData.m
//  sr01sdkProject
//
//  Created by  on 2024/9/3.
//  锻炼期间生命体征数据

#import "CAnAlgExerVitalSignsData.h"

@implementation CAnAlgExerVitalSignsData

-(NSNumber *)dataTimeStamp
{
    return @(self.timeStamp);
}

- (instancetype)initWithData:(NSData *)data
{
    if (self = [super init]) {
        if (data.length != 20) {
            return self;
        }
        
        uint8_t *dataBytes = (uint8_t *)[data bytes];
        _cmdByte = dataBytes[1];
        // 四字节时间戳
        uint32_t timeStamp = *((uint32_t *)(dataBytes + 2));
        _timeStamp = timeStamp;
        
        if (dataBytes[6] != 0XFF) {
            _heartRate = @(dataBytes[6]);
        }
        
        uint16_t hrvTmp = *((uint16_t *)(dataBytes + 7));
        if (hrvTmp != 0XFFFF) {
            _hrv = @(hrvTmp);
        }
        _respirationRate = dataBytes[9];
        
        uint16_t ibi_tmp = *((uint16_t *)(dataBytes + 10));
        if (ibi_tmp != 0XFFFF) {
            _ibi = @(ibi_tmp);
        }
        
     
        
        if (dataBytes[13] <= 100) {
            _cardiacCoherence = @( (dataBytes[13] & 0xff)/100.0f );
        }
        
        if (dataBytes[14] != 0xFF) {
            _vo2 = @(dataBytes[14]);
        }
        if (dataBytes[15] != 0XFF) {
            _vo2Max = @(dataBytes[15]);
        }
        
        uint32_t stress = dataBytes[12] & 0xFF;
        _stressValue = nil;
        if (stress != 0xFF) {
            _stressValue = @(stress / 100.0f);
//            DebugNSLog(@"stress: %@", _stress);
        }
        
        uint32_t skintemp = dataBytes[16] & 0xFF;
        if (skintemp != 0xFF) {
            _skinTemperature = @((skintemp+200)/10.0f );
        }
        if (_stressValue.intValue > 2) {
            DebugNSLog(@"报文错误, %@", _stressValue);
            
        }
        
        // 锻炼类型
        _exerType = dataBytes[17] & 0XFF;
        
        uint8_t status = dataBytes[18];
        _activityType = status & 0x07;
        _keepExeType = (status >> 3) & 0x01;

    }
    
    return self;
}
@end
